package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.Constants;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Ac;
import com.example.exception.CustomException;
import com.example.mapper.AcMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AcService {

    @Resource
    private AcMapper acMapper;

    /**
     * 新增
     */
    public void add(Ac ac) {
        /*Ac dbAc = acMapper.selectByAcname(ac.getAcname());
        if (ObjectUtil.isNotNull(dbAc)) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if (ObjectUtil.isEmpty(ac.getPassword())) {
            ac.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(ac.getName())) {
            ac.setName(ac.getAcname());
        }
        ac.setRole(RoleEnum.USER.name());*/
        acMapper.insert(ac);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        acMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            acMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Ac ac) {
        acMapper.updateById(ac);
    }

    /**
     * 根据ID查询
     */
    public Ac selectById(Integer id) {
        return acMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Ac> selectAll(Ac ac) {
        return acMapper.selectAll(ac);
    }

    /**
     * 分页查询
     */
    public PageInfo<Ac> selectPage(Ac ac, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Ac> list = acMapper.selectAll(ac);
        return PageInfo.of(list);
    }

    /**
     * 登录
     */
    /*public Account login(Account account) {
        Account dbUser = acMapper.selectByAcname(account.getUsername());
        if (ObjectUtil.isNull(dbAc)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbAc.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        // 生成token
        String tokenData = dbAc.getId() + "-" + RoleEnum.USER.name();
        String token = TokenUtils.createToken(tokenData, dbAc.getPassword());
        dbAc.setToken(token);
        return dbAc;

    }*/


    /**
     * 注册
     */
    public void register(Account account) {
       /* Ac ac = new Ac();
        ac.setAcname(account.getAcname());
        ac.setPassword(account.getPassword());
        this.add(ac);*/
    }

    /**
     * 修改密码
     */
    public void updatePassword(Account account) {
        /*Ac dbAc = acMapper.selectByAcname(account.getAcname());
        if (ObjectUtil.isNull(dbAc)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbAc.getPassword())) {
            throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
        }
        dbAc.setPassword(account.getNewPassword());
        acMapper.updateById(dbAc);*/
    }
}
